$(document).ready(function(){
    
        var container = $('div.container');
        
         jQuery.validator.methods.compareLessDate = function(value, element, param) {
            
               var startDate = jQuery(param).val();
               
            if(value=='' || startDate == ''){
                return true;
            }
            var date1 = new Date(Date.parse(startDate.replace(new RegExp('-', 'g'), "/")));
            var date2 = new Date(Date.parse(value.replace(new RegExp('-', 'g'), "/")));
            return date1 <= date2;
        };
        
        jQuery.validator.methods.compareMoreDate = function(value, element, param) {
            
            var endDate = jQuery(param).val();
            
         if(value=='' || endDate == ''){
             return true;
         }
         var date1 = new Date(Date.parse(endDate.replace(new RegExp('-', 'g'), "/")));
         var date2 = new Date(Date.parse(value.replace(new RegExp('-', 'g'), "/")));
         return date1 >= date2;
     };
        
        jQuery.validator.methods.checkyear = function(value, element, param) {
        	if(/^[1-9]{1}\d{3}$/.test(value)){
        	   return true;
        	}
        	return false;  
        };
         
        // validate the form when it is submitted
        validator = $("#form1").validate({
            submitHandler: function(form) {
                form.submit();    
            },
            errorContainer: container,
            errorLabelContainer: $("ol", container),
            wrapper: 'li',
            meta: "validate",
            focusInvalid:false,
            rules: {
                 "fieldMap['planData'].entityId":{  //规划任务库
                      required: true
                    }, 
                    
                "fieldMap['projectName'].value":{  //任务名称
                  required: true, 
                  maxlength:200 
                }, 
                "fieldMap['currentYear'].value":{ //现状年份
                  required: true,
                  checkyear:"#currentYear"
                 }, 
                 "fieldMap['planYear'].value":{ //规划年份
                 required: true
                 }, 
                 "fieldMap['notes'].value":{ //备注
                  maxlength:2000
                 },
                 "fieldMap['editStartDatePlan'].value":{ //编制阶段计划开始日期
                  required: true,
                  compareMoreDate: "#editEndDatePlan"
                 }, 
                 "fieldMap['editEndDatePlan'].value":{ //编制阶段计划结束日期
                  required: true,
                  compareLessDate: "#editStartDatePlan",
                  compareMoreDate: "#auditStartDatePlan"
                 }, 
                 "fieldMap['auditStartDatePlan'].value":{ //评审阶段计划开始日期
                  required: true,
                  compareLessDate: "#editEndDatePlan",
                  compareMoreDate: "#auditEndDatePlan"
                 }, 
                 "fieldMap['auditEndDatePlan'].value":{ //评审阶段计划结束日期
                  required: true,
                  compareLessDate: "#auditStartDatePlan"
                 }
            },
            
            messages:{
                "fieldMap['planData'].entityId":{   //规划任务库
                    required: "请填写规划任务库！"
                },
                "fieldMap['projectName'].value":{   //任务名称
                    required: "请填写任务名称！",
                    maxlength: "任务名称最大不能超过200个字符！"
                },
                "fieldMap['currentYear'].value":{   //现状年份
                    required: "请填写现状年份！",
                    checkyear:"非法的现状年份！正确的格式为YYYY,如：2012。"
                    
                },
                 "fieldMap['planYear'].value":{   //规划年份
                    required: "请填写规划年份！"
                },
                 "fieldMap['notes'].value":{   //备注
                    maxlength: "备注最大不能超过2000个字符！"
                }, 
                 "fieldMap['editStartDatePlan'].value":{ //编制阶段计划开始日期
                  required: "请填写编制开始日期！",
                  compareMoreDate: "编制开始日期必须小于或等于编制结束日期！"
                 }, 
                 "fieldMap['editEndDatePlan'].value":{ //编制阶段计划结束日期
                  required: "请填写编制结束日期！",
                  compareLessDate: "编制结束日期必须大于或等于编制开始日期！",
                  compareMoreDate: "编制结束日期必须小于或等于评审开始日期！"
                 }, 
                 "fieldMap['auditStartDatePlan'].value":{ //评审阶段计划开始日期
                  required: "请填写评审开始日期！",
                  compareLessDate: "评审开始日期必须大于或等于编制结束日期！",
                  compareMoreDate: "评审开始日期必须小于或等于评审结束日期！"
                 }, 
                 "fieldMap['auditEndDatePlan'].value":{ //评审阶段计划结束日期
                  required: "请填写评审结束日期！",
                  compareLessDate: "评审结束日期必须大于或等于评审开始日期！"
                 }
            }
        });
        });
        
        $("#editStartDatePlan").blur(function(){
            var start = $("#editStartDatePlan").val();
            $("#planStartDate").attr("value",start);
            $("#planStartDateLabel").text(start);
            
            var end = $("#auditEndDatePlan").val();
            if(start.length != 0 && end.length != 0){
                var time = DateDiff(end,start);
                $("#workTime").attr("value",time);
                $("#workTimeLabel").text(time);
            }
        });
        
        $("#auditEndDatePlan").blur(function(){
            var end = $("#auditEndDatePlan").val();
            $("#planEndDate").attr("value",end);
            $("#planEndDateLabel").text(end);
            
            var start = $("#editStartDatePlan").val();
            if(start.length != 0 && end.length != 0){
                var time = DateDiff(end,start);
                $("#workTime").attr("value",time);
                $("#workTimeLabel").text(time);
            }
        });
        
        
        /** 
        比较两个日期相差的天数，可为负值 
        **/  
        function DateDiff(sDate1, sDate2)  
        { //sDate1和sDate2是2002-12-18格式    
            var aDate, oDate1, oDate2, iDays;  
            aDate = sDate1.split("-");  
            oDate1 = new Date(aDate[0],aDate[1]-1,aDate[2]);  
            aDate = sDate2.split("-");  
            oDate2 = new Date(aDate[0],aDate[1]-1,aDate[2]);  
              
            iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24);
            if((oDate1 - oDate2)<0){  
                return -iDays;  
            }  
            iDays = iDays + 1;    
            return iDays;   
        }

        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     